/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;

/**
 *
 * @author Hung
 */
public class MD5Hash {

    public static String getHashPassword(String hash) {
        byte[] salt = new byte[12];
        
        
        
        String result;
        SecureRandom random = new SecureRandom();
        random.nextBytes(salt);
        

        String saltstring = asHex(salt);
        result = saltstring + hash;
        return result;
    }
    private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

public static String asHex(byte[] buf)
{
    char[] chars = new char[2 * buf.length];
    for (int i = 0; i < buf.length; ++i)
    {
        chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
        chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
    }
    return new String(chars);
}

    public static boolean authenticate(String md5, String passwordindatabase) {

        boolean login = false;
        
        String password = passwordindatabase.substring(24);
        if(md5.equals(password))
            login = true;

        return login;
    }
   
    public static void main(String arg[]) throws NoSuchAlgorithmException{
        MessageDigest digest = MessageDigest.getInstance("MD5");
        String hung = "1234789";
        digest.update(hung.getBytes());
        byte[] kaka = digest.digest();
        
        System.out.println(MD5Hash.asHex(kaka));
        
        System.out.println(MD5Hash.getHashPassword("81dc9bdb52d04dc20036dbd8313ed055"));
        
//        System.out.println(MD5Hash.authenticate("81dc9bdb52d04dc20036dbd8313ed055", "04ca6b9afc272807e28043bd81dc9bdb52d04dc20036dbd8313ed055"));
//        System.out.println("6f718933d0e4c896dab11ba93831646339626462353264303464633230303336646264383331336564303535".length());
    }
}
